package server.connector;

import server.container.MyService;
import server.pojo.Request;
import server.pojo.Response;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class MyConnector {

    public MyService myService;
    public int port;
    public ServerSocket serverSocket;

    public MyConnector(MyService myService, String port) {
        this.port = Integer.valueOf(port);
        this.myService = myService;
        this.myService.add(this);
    }

    public void start() {
        try {
            serverSocket = new ServerSocket(this.port);
            System.out.println("serversocket done");
            while (true) {
                Socket socket = serverSocket.accept();
                InputStream inputStream = socket.getInputStream();

                // 封装Request对象和Response对象
                Request request = new Request(inputStream);
                Response response = new Response(socket.getOutputStream());
                myService.invoke(request, response);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
